<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item  prop="configName">
        <el-input v-model="queryParams.configName" placeholder="请输入配置名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item  prop="dataBaseVersion">
        <el-input v-model="queryParams.dataBaseVersion" placeholder="请输入数据库版本" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item  prop="dataBaseUrl">
        <el-input v-model="queryParams.dataBaseUrl" placeholder="请输入数据库url" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item  prop="dataBaseUserName">
        <el-input v-model="queryParams.dataBaseUserName" placeholder="请输入用户名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item  prop="dataBasePassword">
        <el-input v-model="queryParams.dataBasePassword" placeholder="请输入密码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"> <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['screwConfig:screwConfig:add']" >新增</el-button> </el-col>
      <el-col :span="1.5"> <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['screwConfig:screwConfig:edit']" >修改</el-button> </el-col>
      <el-col :span="1.5"> <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['screwConfig:screwConfig:remove']" >删除</el-button> </el-col>
      <el-col :span="1.5"> <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['screwConfig:screwConfig:export']" >导出</el-button> </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="screwConfigList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="screwId" />
      <el-table-column label="配置名称" align="center" prop="configName" min-width="100" />
      <el-table-column label="数据库类型" align="center" prop="dataBaseType" min-width="100" />
      <el-table-column label="数据库版本" align="center" prop="dataBaseVersion" min-width="100" />
      <el-table-column label="数据库url" align="center" prop="dataBaseUrl" min-width="400" />
      <el-table-column label="用户名" align="center" prop="dataBaseUserName" min-width="100" />
      <el-table-column label="密码" align="center" prop="dataBasePassword" min-width="100" />
      <el-table-column label="输出类型" align="center" prop="outFileType" min-width="100" />
      <el-table-column label="备注" align="center" prop="remark" min-width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['screwConfig:screwConfig:edit']" >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['screwConfig:screwConfig:remove']" >删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleGenerate(scope.row)" v-hasPermi="['screwConfig:screwConfig:edit']" >生成</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改screwConfig对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="配置名称" prop="configName">
          <el-input v-model="form.configName" placeholder="请输入配置名称" />
        </el-form-item>
        <el-form-item label="数据库类型" prop="dataBaseType">
          <el-input v-model="form.dataBaseType" placeholder="请输入数据库url" />
        </el-form-item>
        <el-form-item label="数据库版本" prop="dataBaseVersion">
          <el-input v-model="form.dataBaseVersion" placeholder="请输入数据库版本" />
        </el-form-item>
        <el-form-item label="数据库url" prop="dataBaseUrl">
          <el-input v-model="form.dataBaseUrl" placeholder="请输入数据库url" />
        </el-form-item>
        <el-form-item label="用户名" prop="dataBaseUserName">
          <el-input v-model="form.dataBaseUserName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="dataBasePassword">
          <el-input v-model="form.dataBasePassword" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="输出类型" prop="outFileType">
          <el-input v-model="form.outFileType" placeholder="请输入数据库url" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listScrewConfig, getScrewConfig, delScrewConfig, addScrewConfig, updateScrewConfig, generateDocumentConfig } from '@/api/system/screwConfig'

  export default {
    name: "ScrewConfig",
    data() {
      return {
        loading: true,     // 遮罩层
        ids: [],   // 选中数组
        single: true,  // 非单个禁用
        multiple: true,    // 非多个禁用
        showSearch: true, // 显示搜索条件
        total: 0, // 总条数
        screwConfigList: [],   // screwConfig表格数据
        title: "",     // 弹出层标题
        open: false,   // 是否显示弹出层

        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          configName: null,
          dataBaseVersion: null,
          dataBaseUrl: null,
          dataBaseUserName: null,
          dataBasePassword: null,
          dataBaseType: null,
          outFileType: null,
        },

        form: {}, // 表单参数

        // 表单校验
        rules: {
          configName: [ { required: true, message: "配置名称不能为空", trigger: "blur" } ],
          dataBaseVersion: [ { required: true, message: "数据库版本不能为空", trigger: "blur" } ],
          dataBaseUrl: [ { required: true, message: "数据库url不能为空", trigger: "blur" } ],
          dataBaseUserName: [ { required: true, message: "用户名不能为空", trigger: "blur" } ],
          dataBasePassword: [ { required: true, message: "密码不能为空", trigger: "blur" } ],
          dataBaseType: [ { required: true, message: "数据库类型不能为空", trigger: "change" } ],
          outFileType: [ { required: true, message: "生成的文件类型不能为空", trigger: "change" } ],
          createBy: [ { required: true, message: "创建者不能为空", trigger: "blur" } ],
        }
      };
    },

    /**
     *初始化
     */
    created() {
      this.getList();
    },

    /**
     *页面方法集合
     */
    methods: {

      /**
       *查询screwConfig列表
       */
      getList() {
        this.loading = true;
        listScrewConfig(this.queryParams).then(response => {
          this.screwConfigList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      /**
       * 取消按钮
       */
      cancel() {
        this.open = false;
        this.reset();
      },

      /**
       * 表单重置
       */
      reset() {
        this.form = {
          screwId: null,
          configName: null,
          dataBaseVersion: null,
          dataBaseUrl: null,
          dataBaseUserName: null,
          dataBasePassword: null,
          dataBaseType: null,
          outFileType: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },


      /**
       * 搜索按钮操作
       */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      /**
       * 重置按钮操作
       */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /**
       * 多选框选中数据
       */
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.screwId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },

      /**
       * 新增按钮操作
       */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加screwConfig";
      },

      /**
       * 修改按钮操作
       */
      handleUpdate(row) {
        this.reset();
        const screwId = row.screwId || this.ids
        getScrewConfig(screwId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改screwConfig";
        });
      },

      /**
       * 提交按钮
       */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.screwId != null) {
              updateScrewConfig(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addScrewConfig(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },

      /**
       * 删除按钮操作
       */
      handleDelete(row) {
        const screwIds = row.screwId || this.ids;
        this.$modal.confirm('是否确认删除screwConfig编号为"' + screwIds + '"的数据项？').then(function() {
          return delScrewConfig(screwIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },

      /**
       * 生成操作
       */
      handleGenerate(row) {
        const screwIds = row.screwId || this.ids;
        this.$modal.confirm('是否确认生成screwConfig编号为"' + screwIds + '"的数据项？').then(function() {
          return generateDocumentConfig(screwIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("生成成功");
        }).catch(() => {});
      },

      /**
       * 导出按钮操作
       */
      handleExport() {
        this.download('system/screwConfig/export', {
          ...this.queryParams
        }, `screwConfig_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
